package com.ruoyi.user.sms.controller;

import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.user.sms.service.SmsService;
import com.ruoyi.user.sms.dto.SmsLoginRequest;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Map;

@RestController
@RequestMapping("/user/sms")
public class SmsController {

    @Resource
    private SmsService smsService;

    @PostMapping("/send")
    public AjaxResult send(@RequestBody Map<String, String> body) {
        String phone = body == null ? null : body.get("phone");
        if (!StringUtils.hasText(phone)) {
            return AjaxResult.error(400, "手机号不能为空");
        }
        smsService.sendLoginCode(phone.trim());
        return AjaxResult.success().put("ok", true);
    }

    @PostMapping("/login")
    public AjaxResult login(@RequestBody SmsLoginRequest req) {
        return smsService.loginBySms(req);
    }
}


